package org.springblade.tms.module.tank.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.tms.module.tank.base.TankService;
import org.springblade.tms.module.tank.importer.TankImporter;
import org.springblade.tms.module.tank.vo.TMTankTankVO;
import org.springblade.tms.module.tank.vo.TankExcelVO;
import org.springblade.tms.module.tank.vo.TankVo;
import org.springblade.tms.module.userinfo.base.DriverManagementService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.tank.controller
 * @className: TankController
 * @author: yr
 * @description: 坦克罐控制类
 * @date: 2022/5/31 16:32
 */
@RestController
@AllArgsConstructor
@RequestMapping("tank")
@Api(value = "坦克罐-yr", tags = "坦克罐接口-yr")
public class TankController {
	@Resource
	private TankService tankService;
	@Resource
	private DriverManagementService driverManagementService;
	@Resource
	private RedisTemplate redisTemplate;
	@Resource
	private Validator validator;
	@Resource
	private ThreadPoolExecutor dynamicTp;
	/**
	 * 分页 坦克罐
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "列表查询", notes = "传入Tank")
	public R<IPage<TMTankTankVO>> list(TankVo tankVo, Query query) {
		return tankService.list(tankVo,query);
	}

	/**
	 * @param tankVo:
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 新增或修改坦克罐
	 * @date 2022/5/31 13:44
	 */
	@ApiLog("新增或修改坦克罐")
	@PostMapping("/saveOrUpdate")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "新增或修改车辆", notes = "传入userInfo")
	public R saveOrUpdate(@Valid @RequestBody TankVo tankVo) {
		return tankService.saveOrUpdate(tankVo,driverManagementService.getTmUserInfo());
	}

	/**
	 * 删除 坦克罐
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@RequestParam String ids) {
		return tankService.delete(Func.toLongList(ids),driverManagementService.getTmUserInfo());
	}

	@ApiLog("导入Excel")
	@PostMapping("/importIntoExcel")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "导入Excel", notes = "传入file")
	public R importIntoExcel(@Valid MultipartFile file) {
		String importNo = UUID.randomUUID().toString();
		ExcelUtil.save(file, new TankImporter(tankService, dynamicTp,validator,driverManagementService.getTmUserInfo(),importNo,redisTemplate,AuthUtil.getUser()) , TankExcelVO.class);
		Object num = redisTemplate.opsForValue().get(importNo);
		redisTemplate.delete(importNo);
		return R.data(num);
	}

	@ApiLog("坦克罐数据模板")
	@GetMapping("/export")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "坦克罐数据模板")
	public void exportNotice(HttpServletResponse response) {
		List<TankExcelVO> list = new ArrayList<>();
		ExcelUtil.export(response, "坦克罐数据模板", "坦克罐数据模板", list, TankExcelVO.class);
	}
}
